#include "client.h"
void sendCmdInfo_train(int sockfd, cmdInfo_t *pcmdinfo) { // 用于发送命令结构体
    train_t train;
    memset(&train, 0, sizeof(train));
    train.length = sizeof(cmdInfo_t);
    memcpy(train.data, pcmdinfo, sizeof(cmdInfo_t));
    printf("train.length = %d\n", train.length);
    cmdInfo_t cmdinfo2;
    memset(&cmdinfo2, 0, sizeof(cmdInfo_t));
    memcpy(&cmdinfo2, train.data, train.length);
    printf("---------------------------from "
           "sendCmdInfo_train------------------------");
    printf("cmdinfo2.cmd = %d\ncmdinfo2.userName = %s\ncmdinfo2.arg = "
           "%s\ncmdinfo2.argLength = %d\n",
           cmdinfo2.cmd, cmdinfo2.userName, cmdinfo2.arg, cmdinfo2.argLength);

    send(sockfd, &train, sizeof(int) + train.length, MSG_NOSIGNAL);
}
int CD_Client(int sockfd, cmdInfo_t *pcmdinfo) {
    if (pcmdinfo->arg == 0) {
        printf("puts 命令的参数有误！没有指定文件\n");
        return -1;
    }
    printf("0\n");
    sendCmdInfo_train(sockfd, pcmdinfo); // 把命令结构体发送给服务端
    printf("1\n");
    int length = 0;
    char errInfo[4] = {0};
    // 接收服务端返回的信息（本次cd成功与否？）
    int ret = recvYesOrNo(sockfd);
    if (ret == true) { // 说明cd成功，要修改工作路径了
        memset(pcmdinfo->cmdPath, 0, sizeof(pcmdinfo->cmdPath));
        length = 0;
        recv(sockfd, &length, sizeof(int), MSG_WAITALL);
        printf("length = %d\n", length);
        recv(sockfd, pcmdinfo->cmdPath, length, MSG_WAITALL);
    } else if (ret == false) {
        printf("cd fail!\n");
        return -1;
    } else {
        printf("cd:unknown error\n");
        return -1;
    }
    printf("3\n");
    printf("after cd,path = %s\n", pcmdinfo->cmdPath);

    printf("CD_Client结束!\n");
    return 0;
}
int LS_Client(int sockfd, cmdInfo_t *pcmdinfo) {
    printf("---------------------from LS_Client1-----------------\n");
    if (pcmdinfo->argLength != 0) {
        printf("ls 命令不应携带参数！\n");
        return -1;
    }
    // 接下来，arg是否正确，就交给服务端来判断
    sendCmdInfo_train(sockfd, pcmdinfo); // 把命令结构体发送给服务端
    printf("---------------------from LS_Client2-----------------\n");
    //--------------------------------------这里要接收返回值，后面再加
    bool ret = recvYesOrNo(sockfd);
    if (ret == false) {
        printf("The current folder has no files\n");
        return -1;
    }
    // 接收服务器发送来的结果
    int length = 0;
    char buf[4096] = {0};
    recv(sockfd, &length, sizeof(int), MSG_WAITALL);
    printf("length = %d\n",length);
    recv(sockfd, buf, length, MSG_WAITALL);
    printf("buf = %s\n", buf);
    char *p;
    /* 第一次调用，传入原始字符串 */
    p = strtok(buf, "$");
    while (p != NULL) {
        printf("%s\n", p); // 打印当前子串
        /* 后续调用，传入NULL */
        p = strtok(NULL, "$");
    }
    printf("LS_Client结束!\n");
    return 0;
}
int REMOVE_Client(int sockfd, cmdInfo_t *pcmdinfo) {
    printf("--------------from REMOVE_Client------------\n");
    if (pcmdinfo->arg == 0) {
        printf("remove 命令的参数有误！没有指定文件或目录\n");
        return -1;
    }
    // 接下来，arg是否正确，就交给服务端来判断
    sendCmdInfo_train(sockfd, pcmdinfo); // 把命令结构体发送给服务端
    int ret = recvYesOrNo(sockfd);
    printf("REMOVE_Client\n");
}
int PWD_Client(int sockfd, cmdInfo_t *pcmdinfo) {
    printf("----------------------from PWD_Client-------------------\n");
    if (pcmdinfo->argLength != 0) {
        printf("pwd 命令不应携带参数！\n");
        return -1;
    }
    /* sendCmdInfo_train(sockfd, pcmdinfo); // 把命令结构体发送给服务端 */
    printf("The current working path is:%s\n", pcmdinfo->cmdPath);
    printf("user_id is:%d\n", pcmdinfo->user_id);
    printf("PWD_Client\n");
}
int MKDIR_Client(int sockfd, cmdInfo_t *pcmdinfo) {
    if (pcmdinfo->arg == 0) {
        printf("mkdir 命令的参数有误！没有指定目录\n");
        return -1;
    }
    printf("---------------------from MKDIR_Client-----------------\n");
    sendCmdInfo_train(sockfd, pcmdinfo); // 把命令结构体发送给服务端
    int ret = recvYesOrNo(sockfd);
    if (ret == false) {
        printf("Directory already exists!\n");
        return -1;
    }
    printf("MKDIR_Client\n");
    return 0;
}
